<?php

namespace common\widgets\pages;

use common\widgets\ActiveForm;
use common\widgets\Box;
use common\widgets\Form;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class FormViewPage extends AdminPage
{
    public $formModel;
    public $model;
    public $tabs;
    public $pageTitle;
    public $extra;
    public $method;
    public $tabTitle = '';

    /**
     * @var string
     */
    protected $themeViewPath = "@common/views/widgets/pages/";

    /**
     * @var string
     */
    protected $pageViewName = 'form-page';

    /**
     * {@inheritdoc}
     */
    public function __construct(array $config)
    {
        parent::__construct($config);
        $this->tabs['title'] = isset($this->pageTitle) ? $this->pageTitle :
            method_exists($this->formModel, "classLabelName") ? $this->formModel->classLabelName() : $this->formModel->className();
        $this->tabs['id'] = 'page-tab-' . md5(microtime(true));
        if ($this->tabTitle === '')
        {
            $this->tabTitle = Yii::t('common', 'Create and Update');
        }
        $this->tabs["items"][] = ["label" => $this->tabTitle];
    }

    public function render($params = [])
    {
        $formContent = '';
        ob_start();
        $form = ActiveForm::begin(
            [
                'type'               => ActiveForm::TYPE_VERTICAL,
                'options'            => ['enctype' => 'multipart/form-data'],
                'enableClientScript' => true,
                //'action' => isset($method) ? \yii\helpers\Url::to($method) : \yii\helpers\url::current()
            ]);

        $extra = isset($extra) ? $extra : null;
        if (method_exists($this->formModel, "formRows") && $formRows = $this->formModel->formRows($form, $this->formModel, $this->extra))
        {
            foreach ($formRows as $formRow)
            {
                $defaultRowConfig = [
                    'model' => $this->formModel,
                    'form'  => $form,
                ];
                $formRow = ArrayHelper::merge($defaultRowConfig, $formRow);
                if ( ! isset($formRow['columns']))
                {
                    $formRow['columns'] = count($formRow['attributes']) > 0 ? count($formRow['attributes']) : 1;
                }

                //特定场景不渲染
                if (isset($formRow['on']) && is_array($formRow['on']))
                {
                    if ( ! in_array($this->formModel->scenario, $formRow['on']))
                    {
                        continue;
                    }
                }

                $formWidget = Form::widget($formRow);
                if (isset($formRow['box']))
                {
                    $formRow['box']['title'] = isset($formRow['box']['title']) ? $formRow['box']['title'] : "";
                    $widgetContent = Box::widget([
                        'title'           => $formRow['box']['title'],
                        'withBorder'      => true,
                        'type'            => Box::TYPE_SUCCESS,
                        'collapse'        => true,
                        'collapseDefault' => true,
                        'content'         => $formWidget,
                    ]);

                }
                else
                {
                    $widgetContent = $formWidget;
                }
                echo $widgetContent;

            }
        }
        //加入表单提交按钮
        echo Html::submitButton(
            $this->formModel->isNewRecord ? Yii::t('common', 'Create') : Yii::t('common', 'Update'),
            ['class' => $this->formModel->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
        );
        ActiveForm::end();
        $params['formContent'] = ob_get_clean();

        $params['_page'] = $this;


        return $this->getController()->render($this->getPageViewPath(), $params);
    }
}
